﻿using System;
using System.Reflection;
using System.Web.Optimization;

using Autofac;
using Autofac.Integration.Mvc;
using Autofac.Integration.WebApi;
using Freedom.Framework.Logging;
using Freedom.Framework.Mvc;
using Freedom.WebDemo.App_Start;

namespace Freedom.WebDemo
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801
    public class MvcApplication : AutofacApplication
    {
        protected override void RegisterServices(ContainerBuilder builder)
        {
            var assembly = Assembly.Load("Freedom.Services");
            builder.RegisterAssemblyTypes(assembly).Where(t => t.Name.EndsWith("Service", StringComparison.Ordinal)).
                AsImplementedInterfaces();
        }

        protected override void RegisterRepository(ContainerBuilder builder)
        {
            var assembly = Assembly.Load("Freedom.Repository");
            builder.RegisterAssemblyTypes(assembly).Where(t => t.Name.EndsWith("Repository", StringComparison.Ordinal)).
                AsImplementedInterfaces();

            //add the Entity Framework context to make sure only one context per request
            builder.RegisterType<Repository.FreedomContext>().InstancePerHttpRequest().InstancePerApiRequest();
        }

        protected override void OnApplicationStarted()
        {
            BundleConfig.RegisterBundles(BundleTable.Bundles);
        }
    }
}